package com.meij.fragment;

import org.json.JSONObject;

import android.view.View;
import android.view.View.OnClickListener;

import com.fragment.BaseFragment;
import com.meij.MJApplaction;
import com.meij.R;
import com.meij.activity.user.UserCenterActivity;
import com.meij.bean.UserInfo;
import com.meij.http.Json;
import com.meij.http.Url;
import com.meij.util.Info;

public class UserInfoFragment extends BaseFragment {
	private UserCenterActivity activity;

	@Override
	protected int getID() {
		return R.layout.fragment_userinfo;
	}

	@Override
	public void onResume() {
		super.onResume();
		editView();
		if (!activity.id.equals(MJApplaction.userInfo.getUserkey())) {
			checkFollow();
		} else {
			aq.id(R.id.button1).text("注销").clicked(new OnClickListener() {
				@Override
				public void onClick(View v) {
					MJApplaction.userInfo = new UserInfo();
					Info.saveUserInfo(activity);
					activity.finish();
				}
			});
		}
	}

	@Override
	protected void editView() {
		activity = (UserCenterActivity) getActivity();
		aq.id(R.id.imageView1).image(Url.ctx + MJApplaction.userInfo.getPerson().getAvatar());
		String nickname = "昵称:" + MJApplaction.userInfo.getPerson().getNickname();
		String attr = "\n\n用户属性:" + MJApplaction.userInfo.getPerson().getAttr();
		String city = "\n\n城市:" + MJApplaction.userInfo.getPerson().getCity();
		String sex = "\n\n性别:" + (MJApplaction.userInfo.getPerson().getSex() == 2 ? "女" : "男");
		String age = "\n\n年龄:" + MJApplaction.userInfo.getPerson().getAge();
		String view_count = "\n\n浏览次数:" + MJApplaction.userInfo.getPerson().getView_count();
		String power = "\n\n战斗力:" + MJApplaction.userInfo.getPerson().getPower();
		String loveliness = "\n\n魅力:" + MJApplaction.userInfo.getPerson().getLoveliness();
		String level_id = "\n\n段位:" + MJApplaction.userInfo.getPerson().getLevel_id();
		String billiard_hall_love = "\n\n常去台球厅:" + MJApplaction.userInfo.getPerson().getBilliard_hall_love();
		String type = "\n\n用户类型:";
		switch (MJApplaction.userInfo.getPerson().getType()) {
		case 1:
			type += "普通";
			break;
		case 2:
			type += "陪练";
			break;
		case 3:
			type += "教练";
			break;
		case 23:
			type += "全能";
			break;
		}
		String fee = "\n\n陪练费用:" + MJApplaction.userInfo.getPerson().getFee();
		String profile = "\n\n个人介绍:" + MJApplaction.userInfo.getPerson().getProfile();
		aq.id(R.id.textView1)
				.text(nickname + attr + city + sex + age + view_count + power + loveliness + level_id + billiard_hall_love + type + fee
						+ profile);
	}

	/***
	 * 检查是否关注了当前用户
	 */
	private void checkFollow() {
		String url = Url.follow_check + "?userkey=" + MJApplaction.userInfo.getUserkey() + "&followed=" + activity.id;
		baseCallBack callBack = new baseCallBack() {
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				boolean isFollowed = Json.getIsFollowed(object);
				changeFollow(isFollowed);
			}
		};
		activity.getJson(url, callBack);
	}

	private void changeFollow(boolean isFollowed) {
		if (isFollowed) {
			aq.id(R.id.button1).text("取消关注").clicked(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String url = Url.unfollow + "?userkey=" + MJApplaction.userInfo.getUserkey() + "&followed=" + activity.id;
					baseCallBack callBack = new baseCallBack() {
						public void onSuccess(JSONObject object) {
							super.onSuccess(object);
							changeFollow(false);
						}
					};
					activity.getJson(url, callBack);
				}
			});
		} else {
			aq.id(R.id.button1).text("添加关注").clicked(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String url = Url.follow + "?userkey=" + MJApplaction.userInfo.getUserkey() + "&followed=" + activity.id;
					baseCallBack callBack = new baseCallBack() {
						public void onSuccess(JSONObject object) {
							super.onSuccess(object);
							changeFollow(true);
						}
					};
					activity.getJson(url, callBack);
				}
			});
		}
	}
}
